﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Diagnostics;
using DesmoPlugin;

namespace DesmoPlugin
{
    public partial class XmlHandler
    {
        public static string[,] XmlSettings = new string[11,350];

        public static string path;
        public static string HBpath;
        XmlDocument doc = new XmlDocument();

        public XmlHandler()
        {
            
            SettingsManagment();
            XmlRefresh();
            
        }

        public void XmlRefresh()
        {
           
            doc.Load(path);
            IEnumerator ie = doc.SelectNodes("/Professionbuddy/SubRoutine/Settings").GetEnumerator();
            int counter = 0;

            ie.Reset();
            while (ie.MoveNext())
            {

                XmlSettings[0, counter] = (ie.Current as XmlNode).Attributes["Name"].Value;
                XmlSettings[1, counter] = (ie.Current as XmlNode).Attributes["Type"].Value;
                XmlSettings[2, counter] = (ie.Current as XmlNode).Attributes["DefaultValue"].Value;
                if ((ie.Current as XmlNode).Attributes["Category"] != null)
                    XmlSettings[3, counter] = (ie.Current as XmlNode).Attributes["Category"].Value;

                if ((ie.Current as XmlNode).Attributes["RunTime"] != null)
                    XmlSettings[4, counter] = (ie.Current as XmlNode).Attributes["RunTime"].Value;

                if ((ie.Current as XmlNode).Attributes["MinBuyout"] != null)
                    XmlSettings[5, counter] = (ie.Current as XmlNode).Attributes["MinBuyout"].Value;

                if ((ie.Current as XmlNode).Attributes["MaxBuyout"] != null)
                    XmlSettings[6, counter] = (ie.Current as XmlNode).Attributes["MaxBuyout"].Value;

                if ((ie.Current as XmlNode).Attributes["FallBackPrice"] != null)
                    XmlSettings[7, counter] = (ie.Current as XmlNode).Attributes["FallBackPrice"].Value;

                if ((ie.Current as XmlNode).Attributes["StackSize"] != null)
                    XmlSettings[8, counter] = (ie.Current as XmlNode).Attributes["StackSize"].Value;

                if ((ie.Current as XmlNode).Attributes["Amount"] != null)
                    XmlSettings[9, counter] = (ie.Current as XmlNode).Attributes["Amount"].Value;

                if ((ie.Current as XmlNode).Attributes["Profession"] != null)
                    XmlSettings[10, counter] = (ie.Current as XmlNode).Attributes["Profession"].Value;

                counter++;

            }

        }

        public void SettingsManagment()
        {
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.Contains("Wow"))
                {
                    path = @"Plugins\DesmoPlugin\Temp3.xml";
                    break;
                }

                else
                    path = @"..\..\..\Temp3.xml";
                if (clsProcess.ProcessName.Contains("Honor"))
                {
                    //HBpath = clsProcess.StartInfo.WorkingDirectory;
                    HBpath = clsProcess.MainModule.FileName; 

                }
                    

            }
        }

        public void SaveXml()
        {
            doc.Save(path);
        }

        public void LoadXml()
        {
            doc.Load(path);
        }

        
    }
}
